package cn.com.swu.mianyangapp.ui.news;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.android.material.tabs.TabLayout;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.com.swu.mianyangapp.R;
import cn.com.swu.mianyangapp.ui.bean.News;
import cn.com.swu.mianyangapp.ui.bean.NewsType;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link NewsShowFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NewsShowFragment extends Fragment {
    String Tag = "OKHttp_SHOW";
    final private String mIp = "192.168.58.15:8081";
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    public Handler mHandler;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;;
    private List<String> mTitleList;
    private List<Fragment> mFragmentList;
    private NewsControllerAdapter mNewsControllerAdapter;
    public NewsShowFragment() {

    }


    public static NewsShowFragment newInstance(String param1, String param2) {
        NewsShowFragment fragment = new NewsShowFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_news_show, container, false);
    }
    @SuppressLint("HandlerLeak")
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        mHandler = new Handler(){

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        mFragmentList = new ArrayList<>();
                        mTitleList = new ArrayList<>();
                        List<NewsType> list = new ArrayList<NewsType>();
                        list = (List<NewsType>)msg.obj;

                        for (int i=0;i<list.size();i++){
                            NewsListFragment newsListFragment = NewsListFragment.newInstance(list.get(i).getNews_type_name(),"");
                            mFragmentList.add(newsListFragment);
                            mTitleList.add(list.get(i).getNews_type_name());
                        }
                        Log.e(Tag,mTitleList.toString());
                        mViewPager = view.findViewById(R.id.news_show_vp);
                        mTabLayout = view.findViewById(R.id.news_show_tab_layout);
                        mNewsControllerAdapter = new NewsControllerAdapter(getChildFragmentManager(),mFragmentList,mTitleList);
                        mViewPager.setAdapter(mNewsControllerAdapter);
                        mTabLayout.setupWithViewPager(mViewPager);
                }

            }
        };

    }

    //todo: add url to get date
    private void initData() {
        OkHttpClient client = new OkHttpClient();
        FormBody body = new FormBody.Builder().build();
        Request request = new Request.Builder()
                .url("http://" + mIp + "/news_type_query")
                .addHeader("Accept-Language","zh-CN,zh;q=0.9")
                .post(body)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                Log.e(Tag,e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                JSONArray jsonArray = JSON.parseArray(result);
                List<NewsType> list = JSONObject.parseArray(jsonArray.toJSONString(), NewsType.class);

                Message msg = Message.obtain();
                Log.e(Tag,list.toString());
                msg.what = 1; // 消息标识
                msg.obj = list; // 消息内存存放
                mHandler.sendMessage(msg);
            }
        });

    }
}